﻿var covericon = "";
var tags = [];

createEditor();

$(function() {
    bindSubmit();

    bindUploadify();

    $("#txt_catalog").catalogs({ dataurl: rootpath + "api/course.ashx", parmsdata: { type: "getcatalogs", pid: 0} });
    $("#dv_tags").tagselector({ dataurl: rootpath + "api/tag.ashx", width: "90%",onSelectChange: getTags });

    formReady();
});

function createEditor() {
    KE.show({
        id: 'txt_description',
        imageUploadJson: '../../../api/upload_json.ashx',
        fileManagerJson: '../../../api/file_manager_json.ashx',
        allowFileManager: true,
        resizeMode: 1,
        shadowMode: true,
        width: '90%',
        height: '260px'
    });
    KE.show({
        id: 'txt_lecturerintro',
        imageUploadJson: '../../../api/upload_json.ashx',
        fileManagerJson: '../../../api/file_manager_json.ashx',
        allowFileManager: true,
        resizeMode: 1,
        shadowMode: true,
        width: '90%',
        height: '260px'
    });
}

function bindUploadify() {
    $("#uploadify").uploadify({
        'uploader': rootpath + 'swf/uploadify.swf',
        'script': 'create.ashx',
        'scriptData': { type: 'uploadcovericon', covericon: covericon },
        'cancelImg': rootpath + 'images/cancel.png',
        'buttonImg': rootpath + 'images/browse-files.png',
        'folder': rootpath + 'data/coursepicture',
        'queueID': 'dv_queue',
        'displayData': 'speed',
        'auto': true,
        'multi': false,
        'width': 130,
        'sizeLimit': 1024 * 1024 * 2,
        'fileDesc': '请选择gif jpg pgn bmp jpeg文件',
        'fileExt': '*.gif;*.jpg;*.png;*.bmp;*.jpeg',
        onSelect: function(event, queueid, fileobj) {
            $.ajax({
                type: "GET",
                url: rootpath + "api/course.ashx",
                data: { type: 'createfilename', r: Math.random() },
                dataType: "string",
                async: false,
                success: function(data) {
                    covericon = data + fileobj.type;
                }
            });

            $("#uploadify").uploadifySettings('scriptData', { 'covericon': covericon });
        },
        onOpen: function() {

        },
        onComplete: function(event, queueid, fileobj) {        
            $("#dv_preview").empty();
            $("<img/>").css({ width: '160px', height: '120px' }).attr("src", getCourseCover(covericon)).appendTo($("#dv_preview"));
            $("#dv_preview").append("&nbsp;");
            $("<input type='button' class='deleteButton' />").click(function() {
                $.ajax({
                    type: "post",
                    url: "create.ashx",
                    data: { type: 'deletecovericon', filename: covericon },
                    dataType: "string",
                    success: function(data) {
                        if (data == 1) {
                            $("#dv_preview").fadeOut("slow");
                            //$("#dv_upload,#dv_queue").show();
                            covericon = "";
                        }
                    }
                });
            }).appendTo($("#dv_preview"));
            $("#dv_preview").fadeIn("slow");
        },
        onError: function() {
            covericon = "";
        },
        onCancel: function() {
            covericon = "";
            $("#uploadify").uploadifyClearQueue();
        }
    });
}

function getTags(tagbox) {
    tags = tagbox;
}

function bindSubmit() {
    $("form").validate({
        rules: {
            txt_coursename: { required: true },
            txt_serialnumber: {
                required: true,
                rangelength: [4, 10],
                remote: {
                    url: rootpath + "course/create/create.ashx",
                    type: "get",
                    data: { type: "checkserialnumber", serialnumber: function() { return $("#txt_serialnumber").val(); }, r: function() { return Math.random() } }
                }
            },
            txt_catalog: { required: true },
            txt_playlength: { digits: true }
        },
        messages: {
            txt_coursename: { required: "课程名称不能为空！" },
            txt_serialnumber: { required: "课程编号不能为空！", rangelength: "课程编号长度需在{0}至{1}位之间", remote: "课程编号已被使用！" },
            txt_catalog: { required: "课程目录不能为空！" },
            txt_playlength: { digits: "课程播放时长必需为整数！" }
        },
        submitHandler: function() {
            $(".btn").attr("disabled", "disabled").after("<span>提交中.....</span>");
            var coursetype = $(":radio[name='coursetype']").filter(":checked").val();
            var ispublic = $(":radio[name='ispublic']").filter(":checked").val();
            var isrecommend = $(":radio[name='isrecommend']").filter(":checked").val();
            var coursename = $("#txt_coursename").val();
            var serialnumber = $("#txt_serialnumber").val();
            var description = KE.html("txt_description");
            var intro = KE.html("txt_lecturerintro");
            var keywords = "";
            var tagids = "";
            $.each(tags, function(i, item) {
                keywords += "|" + item.tname + "|";
                tagids += item.tid + ",";
            });
            var playlength = $("#txt_playlength").val();
            var catalogid = $("#txt_catalog").attr("rel");
            $.ajax({
                type: "post",
                url: rootpath + "course/create/create.ashx",
                data: {
                    type: 'createcourse',
                    cname: coursename,
                    ctype: coursetype,
                    serialnumber: serialnumber,
                    ispublic: ispublic,
                    isrecommend: isrecommend,
                    description: description,
                    intro: intro,
                    keywords: keywords,
                    tagids: tagids,
                    playlength: playlength,
                    catalogid: catalogid,
                    covericon: covericon
                },
                dataType: "string",
                success: function(data) {
                    if (data > 0) {
                        facebox("添加课程成功！");
                        var location = "scorm.aspx?cid=" + data;

                        switch (coursetype) {
                            case "1":
                                location = "scorm.aspx?cid=" + data;
                                break;
                            case "2":
                                location = "nonstandard.aspx?cid=" + data;
                                break;
                        }
                        window.location.href = location;
                    }
                    else {
                        facebox("添加课程失败！");
                    }
                    $(".btn").attr("disabled", "").next("span").remove();
                }
            });
        }
    });
}